package main.java.FSMDE.infrastructure;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.UUID;

import main.java.FSMDE.models.Diagram;
import main.java.FSMDE.models.Interface;
import main.java.FSMDE.models.Transition;


public class ProjectContext implements IProjectContext {

	private static String SubFolder = "Datos";
	private static String Path = "ejemplos";
	
	private List<Diagram> projectDiagrams;
	private List<Diagram> contextDiagrams;

	private String name;

	public ProjectContext() {
		this.projectDiagrams = new ArrayList<Diagram>();
		this.contextDiagrams = new ArrayList<Diagram>();
	}

	public Iterable<Interface> getAllInterfaces() {
		return this.getAllInterfaces(null);
	}

	public Iterable<Transition> getAllTransitions() {
		return this.getAllTransitions(null);
	}

	public Iterable<Interface> getAllInterfaces(Interface interfaceToExclude) {
		return this.getInterfaces(interfaceToExclude, this.projectDiagrams);
	}

	@Override
	public Iterable<Transition> getAllTransitions(Transition transitionToExclude) {
		return this.getTransitions(transitionToExclude, this.projectDiagrams);
	}

	private Iterable<Interface> getInterfaces(Interface interfaceToExclude,
			List<Diagram> diagrams) {

		Set<Interface> interfaces = new HashSet<Interface>();
		if (diagrams == null) {
			if (this.contextDiagrams.isEmpty())
				return interfaces;
			int size = this.contextDiagrams.size();
			for (Interface item : this.contextDiagrams.get(size - 1)
					.getInterfaces())
				interfaces.add(item);
		} else {
			for (Diagram diagram : diagrams) {
				if (interfaceToExclude == null) {
					for (Interface item : diagram.getInterfaces())
						interfaces.add(item);
					continue;
				}
				for (Interface item : diagram.getInterfaces())
					if (!interfaceToExclude.getName().equals(item.getName()))
						interfaces.add(item);
			}
		}
		return interfaces;
	}

	private Iterable<Transition> getTransitions(Transition transitionToExclude,
			List<Diagram> diagrams) {

		Set<Transition> transitions = new HashSet<Transition>();
		if (diagrams == null) {
			if (this.contextDiagrams.isEmpty())
				return transitions;
			int size = this.contextDiagrams.size();
			for (Transition item : this.contextDiagrams.get(size - 1)
					.getTransitions())
				transitions.add(item);
		} else {
			for (Diagram diagram : diagrams) {
				if (transitionToExclude == null) {
					for (Transition item : diagram.getTransitions())
						transitions.add(item);
					continue;
				}
				for (Transition item : diagram.getTransitions())
					if (!transitionToExclude.getName().equals(item.getName()))
						transitions.add(item);
			}
		}
		return transitions;
	}

	@Override
	public void addContextDiagram(Diagram diagram) {
		this.contextDiagrams.add(diagram);
	}

	@Override
	public void addProjectDiagram(Diagram diagram) {
		this.projectDiagrams.add(diagram);
	}

	@Override
	public String getName() {
		return name;
	}

	@Override
	public void setName(String name) {
		this.name = name;
	}

	@Override
	public String getDataDirectory() {
		if (name != null) {
			return Path + "/" + name + "/" + SubFolder;
		}

		return null;
	}

	@Override
	public void clearContextDiagrams() {
		contextDiagrams.clear();
	}

	@Override
	public Iterable<Diagram> getProjectDiagrams() {
		return projectDiagrams;
	}

	@Override
	public Interface getInterface(UUID interfaceId) {
		for (Diagram diagram : projectDiagrams) {
			for (Interface interface0 : diagram.getInterfaces()) {
				if (interface0.getId() == interfaceId) {
					return interface0;
				}
			}
		}
		return null;
	}

	@Override
	public Diagram getContextDiagram(String diagramName) {
		for (Diagram diagram : this.projectDiagrams)
			if (diagram.getName().equals(diagramName))
				return diagram;
		return null;
	}

	@Override
	public void clearProjectDiagrams() {
		this.projectDiagrams.clear();
	}

	@Override
	public void removeDiagram(Diagram diagram) {
		contextDiagrams.remove(diagram);
	}

	@Override
	public Diagram getDeppestContextDiagram() {
		int lastDiagramIndex = contextDiagrams.size() - 1;
		return contextDiagrams.get(lastDiagramIndex);
	}

}
